package com.nick.ls.dao.impl;

import java.sql.Connection;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.mysql.jdbc.Statement;
import com.nick.ls.enity.Category;

public class CategoryDaoImp extends BaseDao implements CategoryDao {

	public List<Category> getCategoryList() {
		Connection connection = null;
		Statement statement = null;
		ResultSet resultSet = null;
		List<Category> result = null;
		try {
			connection = getConn();
			resultSet = statement
					.executeQuery("SELECT category_id, COUNT(category_id) "
							+ "number FROM prodouct GROUP BY category_id ORDER BY category_id");
			result = new ArrayList<Category>();
			while (resultSet.next()) {
				Category category = new Category();
				category.setCategoryId(resultSet.getString("category_id"));
				category.setCategoryNumber(resultSet.getLong("number"));
				result.add(category);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		} finally {
			try {
				close(resultSet, statement, connection);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return result;
	}

	public long getCategoryToatl() {
		Connection connection = null;
		Statement statement = null;
		ResultSet resultSet = null;

		long sum = 0;
		try {
			connection = getConn();
			resultSet = statement
					.executeQuery("select count(category_id) sum from prodouct");
			while (resultSet.next()) {
				sum = resultSet.getLong("sum");
			}
		} catch (Exception e) {
			e.printStackTrace();
			return -1;
		} finally {
			try {
				close(resultSet, statement, connection);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return sum;
	}
}
